#include "Grid.h"
#include "Color.h"

Grid::Grid(float size, float step)
	: _size(size), _step(step)
{
	CvarSystem& cvars = CvarSystem::getInstance();

	_drawAxis = cvars.addCvar("s_axis", true);
	_drawGrid = cvars.addCvar("s_grid", true);
}

void Grid::render(RendererPtr renderer)
{
	renderer->setModelTransform(Mat4());

	if (_drawAxis->getValueB()) {
		// x axe
		renderer->drawLine(Vec3(-_size*2, 0, 0), Vec3(0, 0, 0), MAROON); // - 
		renderer->drawLine(Vec3(0, 0, 0), Vec3(_size*2, 0, 0), RED); // +

		// x axe
		renderer->drawLine(Vec3(0, -_size* 2, 0), Vec3(0, 0, 0), GREEN); // -
		renderer->drawLine(Vec3(0, 0, 0), Vec3(0, _size* 2, 0), LIGHTGREEN); // +

		// z axe
		renderer->drawLine(Vec3(0, 0, -_size*2), Vec3(0, 0, 0), DARKBLUE); // -
		renderer->drawLine(Vec3(0, 0, 0), Vec3(0, 0, _size*2), BLUE); // +
	}

	if (_drawGrid->getValueB()) {
		for(float i = _step; i < _size; i += _step) {
			// parallel to x
			renderer->drawLine(Vec3(-_size, 0, i), Vec3(_size, 0, i), LIGHTGRAY); 
			renderer->drawLine(Vec3(-_size, 0, -i), Vec3(_size, 0, -i), LIGHTGRAY);

			// parallel to z
			renderer->drawLine(Vec3(i, 0, -_size), Vec3(i, 0, _size), LIGHTGRAY);
			renderer->drawLine(Vec3(-i, 0, -_size), Vec3(-i, 0, _size), LIGHTGRAY);
		}
	}
}